﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProductManagement.Infrstructre
{
    /// <summary>
    /// 产品类型仓储层
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ProductsTypeRepository<T> : IProductsTypeRepository<T> where T : class, new()
    {
        ISqlSugarClient db;

        public ProductsTypeRepository(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T entity)
        {
            return await db.Insertable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T entity)
        {
            return await db.Updateable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<ISugarQueryable<T>> GetAll()
        {
            return db.Queryable<T>();
        }

        /// <summary>
        /// 没有异步获取所有数据
        /// </summary>
        /// <returns></returns>
        public List<T> Query()
        {
            return db.Queryable<T>().ToList();
        }

        /// <summary>
        /// 批量真删除
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> DelRange(List<T> entities)
        {
            return db.Deleteable(entities).ExecuteCommand();
        }
    }
}
